﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.AdvTree;
using PPI.Client.Logic;
using PPI.Client.Logic.Inspection;
using PPI.Client.Logic.Utility;
using PPI.Client.Utils;
using PPI.Entities;
using PPI.Entities.Inspections;
using PPI.Entities.Utility;
using ColumnHeader = DevComponents.AdvTree.ColumnHeader;

namespace PPI.Controls.Grid
{
    public partial class TreeGridAsset : TreeGridBase
    {
        protected List<EUtility_Asset_XMissionStructure> m_ListAssets;
        public List<EUtility_Asset_XMissionStructure> ListAssets
        {
            get { return m_ListAssets; }
            set { m_ListAssets = value; }
        }

        public TreeGridAsset()
        {
            InitializeComponent();
        }

        protected override bool ShowMenu()
        {
            return false;
        }

        protected override void ShowMenuOptions()
        {
            cmiAssociateUtility.Visible = false;
            cmiEdit.Visible = false;
            cmiDelete.Visible = false;
            cmiEditWhithOutImages.Visible = false;
            cmiCreateAsset.Visible = false;
        }

        protected override void LoadTree()
        {

            treeGrid.Nodes.Clear();
            treeGrid.Columns.Clear();
            m_ChildElementStyle.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(234)))), ((int)(((byte)(239)))), ((int)(((byte)(242)))));


            #region Columns

            #region BasePositionCollumns

            ColumnHeader chIdAsset = new ColumnHeader("Asset Id");
            chIdAsset.Width.Absolute = 100;

            ColumnHeader chLatitude = new ColumnHeader("Latitude");
            chLatitude.Width.Absolute = 100;

            ColumnHeader chLongitude = new ColumnHeader("Longitude");
            chLongitude.Width.Absolute = 100;

            ColumnHeader chAltitude = new ColumnHeader("Altitude");
            chAltitude.Width.Absolute = 100;

            ColumnHeader chSatelliteCount = new ColumnHeader("Satellites");
            chSatelliteCount.Width.Absolute = 100;

            ColumnHeader chSatelliteTime = new ColumnHeader("Time");
            chSatelliteTime.Width.Absolute = 100;

            ColumnHeader chDate = new ColumnHeader("Date");
            chDate.Width.Absolute = 100;



            #endregion

            ColumnHeader chUtility = new ColumnHeader("Utility");
            chUtility.Width.Absolute = 100;

            ColumnHeader chProject = new ColumnHeader("Project");
            chProject.Width.Absolute = 100;

            ColumnHeader chContact = new ColumnHeader("Contact");
            chContact.Width.Absolute = 100;

            ColumnHeader chTopType = new ColumnHeader("Top Type");
            chTopType.Width.Absolute = 100;

            #endregion


            treeGrid.Columns.Add(chIdAsset);
            treeGrid.Columns.Add(chLatitude);
            treeGrid.Columns.Add(chLongitude);
            treeGrid.Columns.Add(chAltitude);
            treeGrid.Columns.Add(chSatelliteCount);
            treeGrid.Columns.Add(chSatelliteTime);
            treeGrid.Columns.Add(chDate);

            treeGrid.Columns.Add(chUtility);
            treeGrid.Columns.Add(chProject);
            treeGrid.Columns.Add(chContact);
            treeGrid.Columns.Add(chTopType);

            foreach (EUtility_Asset_XMissionStructure asset in m_ListAssets)
            {
                Node visitNode = CreateAssetNode(asset);
                treeGrid.Nodes.Add(visitNode);
            }



        }

        public void LoadAssets(string connectionString)
        {
            m_ListAssets = LUtility_Asset_XMissionStructure.SelectAllWithInspectionGps(connectionString);
            LoadTree();

        }

        private Node CreateAssetNode(EUtility_Asset_XMissionStructure asset)
        {
            Node assetNode = new Node();
            assetNode.DataKey = asset;
            assetNode.Text = asset.Id.ToString();


            
            if (asset.Latitude != double.MinValue)
                assetNode.Cells.Add(new Cell(asset.Latitude.ToString()));
            else
                assetNode.Cells.Add(new Cell(""));

            if (asset.Longitude != double.MinValue)
                assetNode.Cells.Add(new Cell(asset.Longitude.ToString()));
            else
                assetNode.Cells.Add(new Cell(""));


            if (asset.Altitude != double.MinValue)
                assetNode.Cells.Add(new Cell(asset.Altitude.ToString()));
            else
                assetNode.Cells.Add(new Cell(""));

            if (asset.SatelliteCount > 0)
                assetNode.Cells.Add(new Cell(asset.SatelliteCount.ToString()));
            else
                assetNode.Cells.Add(new Cell("0"));

            if (asset.SatelliteTime != DateTime.MinValue)
                assetNode.Cells.Add(new Cell(asset.SatelliteTime.ToLongDateString()));
            else
                assetNode.Cells.Add(new Cell(""));

            if (asset.Date != DateTime.MinValue)
                assetNode.Cells.Add(new Cell(asset.Date.ToLongDateString()));
            else
                assetNode.Cells.Add(new Cell(""));

            assetNode.Cells.Add(new Cell(asset.Utility.Name));
            assetNode.Cells.Add(new Cell(asset.Project.Name));
            assetNode.Cells.Add(new Cell(asset.Contact.Name));


            #region ChildCollumns

            #region BasePositionCollumns

            ColumnHeader chLatitude = new ColumnHeader("Latitude");
            chLatitude.Width.Absolute = 100;

            ColumnHeader chLongitude = new ColumnHeader("Longitude");
            chLongitude.Width.Absolute = 100;

            ColumnHeader chAltitude = new ColumnHeader("Altitude");
            chAltitude.Width.Absolute = 100;

            ColumnHeader chSatelliteCount = new ColumnHeader("Satellites");
            chSatelliteCount.Width.Absolute = 100;

            ColumnHeader chSatelliteTime = new ColumnHeader("Time");
            chSatelliteTime.Width.Absolute = 100;

            ColumnHeader chDate = new ColumnHeader("Date");
            chDate.Width.Absolute = 100;



            #endregion

            ColumnHeader chChildId = new ColumnHeader("Visit ID");
            chChildId.Width.Absolute = 100;

            ColumnHeader chProject = new ColumnHeader("Project");
            chProject.Width.Absolute = 100;

            ColumnHeader chContact = new ColumnHeader("Contact");
            chContact.Width.Absolute = 100;

            ColumnHeader chVisitType = new ColumnHeader("Visit");
            chVisitType.Width.Absolute = 100;

            assetNode.NodesColumns.Add(chVisitType);
            assetNode.NodesColumns.Add(chChildId);
            assetNode.NodesColumns.Add(chDate);
            assetNode.NodesColumns.Add(chProject);
            assetNode.NodesColumns.Add(chLatitude);
            assetNode.NodesColumns.Add(chLongitude);
            
            assetNode.NodesColumns.Add(chSatelliteCount);
            assetNode.NodesColumns.Add(chSatelliteTime);
            assetNode.NodesColumns.Add(chAltitude);
            assetNode.NodesColumns.Add(chContact);

            #endregion

            foreach (EInspectionStructure inspection in asset.VisitInspections)
            {
                Node poleNode = new Node();
                poleNode.Style = m_ChildElementStyle;
                poleNode.Text = "Inspection";
                poleNode.DataKey = inspection;

                poleNode.Cells.Add(new Cell(inspection.Id.ToString()));
                if (inspection.Date != DateTime.MinValue)
                    poleNode.Cells.Add(new Cell(inspection.Date.ToLongDateString()));
                else
                    poleNode.Cells.Add(new Cell(""));

                poleNode.Cells.Add(new Cell(inspection.Project.Name));
                
                if (inspection.Latitude != double.MinValue)
                    poleNode.Cells.Add(new Cell(inspection.Latitude.ToString()));
                else
                    poleNode.Cells.Add(new Cell(""));

                if (inspection.Longitude != double.MinValue)
                    poleNode.Cells.Add(new Cell(inspection.Longitude.ToString()));
                else
                    poleNode.Cells.Add(new Cell(""));

                if (inspection.SatelliteCount > 0)
                    poleNode.Cells.Add(new Cell(inspection.SatelliteCount.ToString()));
                else
                    poleNode.Cells.Add(new Cell("0"));
                if (inspection.SatelliteTime != DateTime.MinValue)
                    poleNode.Cells.Add(new Cell(inspection.SatelliteTime.ToLongDateString()));
                else
                    poleNode.Cells.Add(new Cell(""));

                if (inspection.Altitude != double.MinValue)
                    poleNode.Cells.Add(new Cell(inspection.Altitude.ToString()));
                else
                    poleNode.Cells.Add(new Cell(""));

                poleNode.Cells.Add(new Cell(inspection.Contact.Name));
                
                assetNode.Nodes.Add(poleNode);
            }

            foreach (EVendor_Visit_XMissionStructure gps in asset.VisitGps)
            {
                Node poleNode = new Node();
                poleNode.Style = m_ChildElementStyle;
                poleNode.Text = "GPS";
                poleNode.DataKey = gps;

                poleNode.Cells.Add(new Cell(gps.Id.ToString()));

                if (gps.Date != DateTime.MinValue)
                    poleNode.Cells.Add(new Cell(gps.Date.ToLongDateString()));
                else
                    poleNode.Cells.Add(new Cell(""));
                poleNode.Cells.Add(new Cell(gps.Vendor_Project.Name));

                if (gps.Latitude != double.MinValue)
                    poleNode.Cells.Add(new Cell(gps.Latitude.ToString()));
                else
                    poleNode.Cells.Add(new Cell(""));

                if (gps.Longitude != double.MinValue)
                    poleNode.Cells.Add(new Cell(gps.Longitude.ToString()));
                else
                    poleNode.Cells.Add(new Cell(""));


                if (gps.SatelliteCount > 0)
                    poleNode.Cells.Add(new Cell(gps.SatelliteCount.ToString()));
                else
                    poleNode.Cells.Add(new Cell("0"));
                if (gps.SatelliteTime != DateTime.MinValue)
                    poleNode.Cells.Add(new Cell(gps.SatelliteTime.ToLongDateString()));
                else
                    poleNode.Cells.Add(new Cell(""));


                if (gps.Altitude != double.MinValue)
                    poleNode.Cells.Add(new Cell(gps.Altitude.ToString()));
                else
                    poleNode.Cells.Add(new Cell(""));
                
                poleNode.Cells.Add(new Cell(gps.Vendor_Contact.Name));
                assetNode.Nodes.Add(poleNode);
            }
            return assetNode;
        }

        protected override void BeforeNodeChangeParent(Node currentNode, Node oldParentNode, Node newParentNode)
        {
            EUtility_Asset_XMissionStructure tmpUtilityOld = (EUtility_Asset_XMissionStructure) oldParentNode.DataKey;
            EUtility_Asset_XMissionStructure tmpUtilityNew = (EUtility_Asset_XMissionStructure) newParentNode.DataKey;
            EBasePosition tmpChild = (EBasePosition) currentNode.DataKey;

            if(tmpChild is EInspectionStructure)
            {
                LInspectionStructure.SetAsset((EInspectionStructure)tmpChild, tmpUtilityNew);
                return;
            }

            if(tmpChild is EVendor_Visit_XMissionStructure)
            {
                LVendor_Visit_XMissionStructure.SetAsset((EVendor_Visit_XMissionStructure)tmpChild, tmpUtilityNew);
                return;
            }
        }

        protected override bool CancelActionChangeParent(Node currentNode, Node oldParentNode, Node newParentNode)
        {
            try
            {
                if (oldParentNode.DataKey is EUtility_Asset_XMissionStructure && newParentNode.DataKey is EUtility_Asset_XMissionStructure && currentNode.DataKey is EBasePosition)
                    return false;
                else
                    return true;
            }
            catch(Exception ex)
            {
                return true;
            }
        }
    }
}
